package trunk.frontend;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.*;

import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class CheckboxOptions extends JFrame implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JButton jbSubmit;
	private JButton jbCancel;
	private JTextField jtfNumCheckboxes;
	private ArrayList alExtraBoxesLabels;
	private JTextField[] jtfCheckboxNames;
	
	private GridBagLayout gbl;
	private GridBagConstraints gbc;
	
	private WFBFrame parent;
	
	public CheckboxOptions(WFBFrame myParent) {
		super("Add Checkbox");
		JLabel jlNumBoxes = new JLabel("How many checkboxes?");
		gbl = new GridBagLayout();
		gbc = new GridBagConstraints();
		jtfNumCheckboxes = new JTextField(5);
		alExtraBoxesLabels = new ArrayList();
		jbSubmit = new JButton("Submit");
		jbCancel = new JButton("Cancel");
		JLabel jlWhatContents = new JLabel("Fill in Contents Here");
		jtfCheckboxNames = new JTextField[7];
		
		setPreferredSize(new Dimension(600, 500));
		setSize(new Dimension(600, 500));
		
		jbSubmit.addActionListener(this);
		jbCancel.addActionListener(this);
		
		getContentPane().setLayout(gbl);
		
		gbc.anchor = GridBagConstraints.NORTH;
		gbc.gridx = GridBagConstraints.RELATIVE;
		gbc.gridy = 0;
		
		getContentPane().setLayout(gbl);
		getContentPane().add(jlNumBoxes, gbc);
		getContentPane().add(jtfNumCheckboxes, gbc);
		gbc.gridy = 1;
		getContentPane().add(jlWhatContents, gbc);
		for (int i = 0; i < 7; i++) {
			jtfCheckboxNames[i] = new JTextField(12);
			gbc.gridy = i + 2;
			getContentPane().add(jtfCheckboxNames[i], gbc);
		}
		
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridy = 9;
		
		getContentPane().add(jbSubmit, gbc);
		getContentPane().add(jbCancel, gbc);
		
		parent = myParent;
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource() == jbSubmit) {
			String s = "2#:@name#:@";
			int numCheckboxes = Integer.valueOf(jtfNumCheckboxes.getText());
			
			for(int i = 0; i < numCheckboxes; i++) {
				s += jtfCheckboxNames[i].getText() + "#:@";
			}
			
			s += "#:%";
			
			for(int i = 0; i < numCheckboxes; i++) {
				
				s += i+ "#:@";
			}
			System.out.println(s);
			parent.addToForm(s);
			dispose();
		}
		else if (ae.getSource() == jbCancel) {
			dispose();
		}
	}
}
